package pt.cp.commands;

import design.patterns.command.Command;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

public class ParseTimeTable extends Command<List<GetTimeTables.Item>> {
    public static final String PATH = "TestGetTimeTable.1393197571620.html";

    @Override
    protected void protectedExecute() throws Exception {
        final Document document = Jsoup.parse(new File(PATH), "UTF-8", "./");

        final Elements trs = document
                .select("table.fd_content").get(1)
                .select("tr");

        List<GetTimeTables.Item> elements = new ArrayList<>();

        boolean first = true;
        for (Element tr : trs){
            Elements tds = tr.select("td");

            if (tds.size() != 9)
                continue;

            if (first){
                first = false;
                continue;
            }

            elements.add(new GetTimeTables.Item(tds));
        }

        setResult(elements);
    }

}
